package org.jeecg.modules.product.risk.service.impl;

import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.product.risk.entity.TbBaseCheckPlan;
import org.jeecg.modules.product.risk.entity.TbBaseCheckPlanApprovalRecord;
import org.jeecg.modules.product.risk.entity.TbBaseCheckRecord;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckPlanApprovalRecordMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckPlanMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckRecordMapper;
import org.jeecg.modules.product.risk.service.ITbBaseCheckPlanService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 检查计划
 * @Author: jeecg-boot
 * @Date:   2024-05-17
 * @Version: V1.0
 */
@Service
public class TbBaseCheckPlanServiceImpl extends ServiceImpl<TbBaseCheckPlanMapper, TbBaseCheckPlan> implements ITbBaseCheckPlanService {

	@Autowired
	private TbBaseCheckPlanMapper tbBaseCheckPlanMapper;
	@Autowired
	private TbBaseCheckRecordMapper tbBaseCheckRecordMapper;
	@Autowired
	private TbBaseCheckPlanApprovalRecordMapper tbBaseCheckPlanApprovalRecordMapper;
	
	@Override
	@Transactional
	public void saveMain(TbBaseCheckPlan tbBaseCheckPlan, List<TbBaseCheckRecord> tbBaseCheckRecordList, List<TbBaseCheckPlanApprovalRecord> tbBaseCheckPlanApprovalRecordList) {
		tbBaseCheckPlan.setId(UUIDGenerator.generate());
		tbBaseCheckPlanMapper.insert(tbBaseCheckPlan);
		if(tbBaseCheckRecordList!=null && tbBaseCheckRecordList.size()>0) {
			for(TbBaseCheckRecord entity:tbBaseCheckRecordList) {
				//外键设置
				entity.setPlanId(tbBaseCheckPlan.getId());
				entity.setId(UUIDGenerator.generate());
				tbBaseCheckRecordMapper.insert(entity);
			}
		}
		if(tbBaseCheckPlanApprovalRecordList!=null && tbBaseCheckPlanApprovalRecordList.size()>0) {
			for(TbBaseCheckPlanApprovalRecord entity:tbBaseCheckPlanApprovalRecordList) {
				//外键设置
				entity.setPlanId(tbBaseCheckPlan.getId());
				tbBaseCheckPlanApprovalRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(TbBaseCheckPlan tbBaseCheckPlan,List<TbBaseCheckRecord> tbBaseCheckRecordList,List<TbBaseCheckPlanApprovalRecord> tbBaseCheckPlanApprovalRecordList) {
		tbBaseCheckPlanMapper.updateById(tbBaseCheckPlan);
		
		//1.先删除子表数据
		tbBaseCheckRecordMapper.deleteByMainId(tbBaseCheckPlan.getId());
		tbBaseCheckPlanApprovalRecordMapper.deleteByMainId(tbBaseCheckPlan.getId());
		
		//2.子表数据重新插入
		if(tbBaseCheckRecordList!=null && tbBaseCheckRecordList.size()>0) {
			for(TbBaseCheckRecord entity:tbBaseCheckRecordList) {
				//外键设置
				entity.setPlanId(tbBaseCheckPlan.getId());
				entity.setId(UUIDGenerator.generate());
				tbBaseCheckRecordMapper.insert(entity);
			}
		}
		if(tbBaseCheckPlanApprovalRecordList!=null && tbBaseCheckPlanApprovalRecordList.size()>0) {
			for(TbBaseCheckPlanApprovalRecord entity:tbBaseCheckPlanApprovalRecordList) {
				//外键设置
				entity.setPlanId(tbBaseCheckPlan.getId());
				tbBaseCheckPlanApprovalRecordMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		tbBaseCheckRecordMapper.deleteByMainId(id);
		tbBaseCheckPlanApprovalRecordMapper.deleteByMainId(id);
		tbBaseCheckPlanMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tbBaseCheckRecordMapper.deleteByMainId(id.toString());
			tbBaseCheckPlanApprovalRecordMapper.deleteByMainId(id.toString());
			tbBaseCheckPlanMapper.deleteById(id);
		}
	}
	
}
